/* 
 * File:   Cell.cpp
 * Author: Guilherme Monteiro
 */

#ifndef CELL_H
#define	CELL_H

#include "Line.h"

/**
 * Struct that represents a cell
 * of the table.
 */
struct Cell {
    CvPoint leftUpper;
    CvPoint rightBottom;

    Line up;
    Line down;
    Line left;
    Line right;
    
    /**
     * Constructor of struct
     * Cell.
     * 
     * @param lUpper
     * @param rBottom
     */
    Cell(CvPoint lUpper, CvPoint rBottom) {
        leftUpper = lUpper;
        rightBottom = rBottom;
        
        setUpLine(Line(leftUpper, cvPoint(rightBottom.x, leftUpper.y)));
        setDownLine(Line(cvPoint(leftUpper.x, rightBottom.y), rightBottom));
        setLeftLine(Line(leftUpper, cvPoint(leftUpper.x, rightBottom.y)));
        setRightLine(Line(cvPoint(rightBottom.x, leftUpper.y), rightBottom));
    }
    
    /**
     * Sets the up line.
     * 
     * @param l
     */
    void setUpLine(Line l) {
        this->up.p1 = l.p1;
        this->up.p2 = l.p2;
    }
    
    /**
     * Sets the down line.
     * 
     * @param l
     */
    void setDownLine(Line l) {
        this->down.p1 = l.p1;
        this->down.p2 = l.p2;
    }
    
    /**
     * Sets the left line.
     * 
     * @param l
     */
    void setLeftLine(Line l) {
        this->left.p1 = l.p1;
        this->left.p2 = l.p2;
    }
    
    /**
     * Sets the right line.
     * 
     * @param l
     */
    void setRightLine(Line l) {
        this->right.p1 = l.p1;
        this->right.p2 = l.p2;
    }
    
    /**
     * Returns if this cells
     * and other cells are equals.
     * 
     * @param otherCell
     * @return true if are equals
     */
    bool equals(Cell otherCell) {
        return this->leftUpper.x == otherCell.leftUpper.x &&
                this->leftUpper.y == otherCell.leftUpper.y &&
                this->rightBottom.x == otherCell.rightBottom.x &&
                this->rightBottom.y == otherCell.rightBottom.y;
    }
    
    /**
     * Verify if this cell contains
     * other cell.
     * 
     * @param otherCell
     * @return true if this cell contains the other
     */
    bool contains(Cell* otherCell) {
        return this->leftUpper.x <= otherCell->leftUpper.x &&
                this->leftUpper.y <= otherCell->leftUpper.y &&
                this->rightBottom.x >= otherCell->rightBottom.x &&
                this->rightBottom.y >= otherCell->rightBottom.y;
        
    }
    
    /**
     * Returns if this cell
     * is a valid cell. A valid
     * cell is defined as:
     * cell.RB.x - cell.LU.x > 0 AND
     * cell.RB.y - cell.LU.y > 0
     * 
     * @return true if this cell is valid
     */
    bool isValid() {
        return (this->rightBottom.x - this->leftUpper.x > 0) && 
                (this->rightBottom.y - this->leftUpper.y > 0);
    }
};

#endif	/* CELL_H */
